package org.hhh.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hhh.basic.util.AjaxResult;
import org.hhh.basic.util.PageList;
import org.hhh.org.domain.Department;
import org.hhh.org.query.DepartmentQuery;
import org.hhh.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 部门管理接口数据对接json格式
 */

@RestController
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    //新增或修改
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    @PostMapping("/save")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId()==null){
                departmentService.add(department);
            }else {
                departmentService.upadate(department);
            }
             return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false)
                    .setMessage("系统繁忙啊，请稍后");
        }
    }
    /**
     * 写接口三要素
     * 1.请求地址是什么
     * 2.请求的参数
     * 3.返回值是什么
     * @param id
     * @return
     */
    //删除
    @DeleteMapping("/{id}")
    public AjaxResult removeId(@PathVariable("id") Long id){
        try {
            departmentService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false)
                    .setMessage("系统繁忙,殴打程序猿");
        }
    }

    //批量删除
    @PatchMapping
    public AjaxResult patchTree(@RequestBody List<Long> ids){
        try {
            departmentService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false)
                    .setMessage("系统繁忙,殴打程序猿");
        }
    }

    //查询部门一条数据
    @GetMapping("/{id}")
    public Department getById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }
    //查询全部数据
    @GetMapping("/list")
    public List<Department> getAll(){
        return departmentService.findAll();
    }

    //分页+高级查询
    @PostMapping("/page")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query, HttpServletRequest request){
        System.out.println(request.getHeader("token"));
        return departmentService.queryPage(query);
    }

    //查询部门树 一级+二级
    @GetMapping("/tree")
    public List<Department> findTree(){
        return departmentService.findOneAndTwoTree();
    }

}
